package com.isoft.controller.gate;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isoft.bean.ResponseData;
import com.isoft.controller.BaseController;
import com.isoft.entity.ball.Blls;
import com.isoft.entity.flight.AirportInfo;
import com.isoft.entity.gate.Gate;
import com.isoft.service.gate.BoardingGateService;
import com.isoft.service.gate.impl.BoardingGateServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/board")
@CrossOrigin
public class BoardingGateController extends BaseController {
    @Autowired
    BoardingGateServiceImpl boardingGateService;
    //查询所有登机口信息
    @GetMapping("/all")
    public ResponseData<Object> selectAllBoardingGate() {
        return returnObject(boardingGateService.findAllBoardingGate());
    }
    //根据META——ID模糊查询登机口信息
    @GetMapping("/findBy")
    public ResponseData<List<Gate>> getGateBy(
            @RequestParam(value = "META_ID", required = false) Integer META_ID,
            @RequestParam(value = "CODE", required = false) String CODE,
            @RequestParam(defaultValue = "1") int currentPage,
            @RequestParam(defaultValue = "7") int pageSize) {
        //使用mybatis-plus的分页查询
        Page<Gate> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Gate> queryWrapper = new QueryWrapper<>();
        //倒序
        queryWrapper.orderByDesc("ID");
        if (META_ID != null &&  META_ID!=0) {
            queryWrapper.like("META_ID", META_ID);
        }
        if (CODE != null && !CODE.isEmpty()) {
            queryWrapper.like("CODE", CODE);
        }
        boardingGateService.page(page, queryWrapper);
        return ResponseData.success(page.getRecords(), page.getTotal());
    }
//分页展示所有
    @GetMapping("/page")
    public ResponseData<List<Gate>> getBllsInfosPage(
            @RequestParam(defaultValue = "1") int currentPage,
            @RequestParam(defaultValue = "5") int pageSize) {
        try {
            // 调用服务层获取分页数据
            IPage<Gate> gatePage = boardingGateService.getBllsInfosPage(currentPage, pageSize);

            // 从IPage中提取数据列表和总记录数
            List<Gate> dataList = gatePage.getRecords(); // 获取数据列表
            long total = gatePage.getTotal(); // 获取总记录数

            // 封装到ResponseData中
            return ResponseData.success(dataList, total);
        } catch (Exception e) {
            // 异常处理，返回错误信息
            return ResponseData.failure("500", "获取Gate信息失败: " + e.getMessage());
        }
    }
//    删除
    @DeleteMapping("/{ID}")
    public ResponseData<Boolean> deleteBoardingGate(@PathVariable Integer ID) {
        try {
            // 调用服务层删除登机口信息
            boolean result = boardingGateService.deleteBoardingGate(ID);

            // 根据删除结果返回响应
            if (result) {
                return returnBoolean(true, "200", "登机口信息删除成功");
            } else {
                return ResponseData.failure("404", "登机口信息不存在");
            }
        } catch (Exception e) {
            // 异常处理，返回错误信息
            return ResponseData.failure("500", "删除登机口信息失败: " + e.getMessage());
        }
    }

//    修改
    @PutMapping("/update")
    public ResponseData<Boolean> updateBoardingGate(@RequestBody Gate gate) {
        try {
            // 调用服务层修改登机口信息
            boolean result = boardingGateService.updateBoardingGate(gate);

            // 根据修改结果返回响应
            if (result) {
                return returnBoolean(true, "200", "登机口信息修改成功");
            } else {
                return ResponseData.failure("404", "登机口信息不存在");
            }
        } catch (Exception e) {
           // 异常处理，返回错误信息
            return ResponseData.failure("500", "修改登机口信息失败: " + e.getMessage());
       }
   }
//   添加
    @PostMapping("/insert")
    public ResponseData<Boolean> insertBoardingGate(@RequestBody Gate gate) {
        try {
            // 调用服务层添加登机口信息
            Boolean result = boardingGateService.insertBoardingGate(gate);

            // 根据添加结果返回响应
            if(result){
                return returnBoolean(result, "200", "添加成功");
            }else {
                return returnBoolean(result, "500", "添加失败！该航班号信息已存在");
            }
        } catch (Exception e) {
            // 异常处理，返回错误信息
            return ResponseData.failure("500", "添加登机口信息失败: " + e.getMessage());
        }
    }
}
